// HSS.Forms.Docking.DockPaneCaptionBase.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DockPaneCaptionBase.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms.Docking
{
	#region Using Directives
	using System.Drawing;
	using System.Security.Permissions;
	using System.Windows.Forms;
	#endregion

	#region DockPaneCaptionBase
	/// <summary>
	/// DockPaneCaptionBase
	/// </summary>
	public abstract class DockPaneCaptionBase : Control
	{
        #region Constructor
		/// <summary>
		/// ctor
		/// </summary>
		/// <param name="pane">DockPane</param>
		protected internal DockPaneCaptionBase(DockPane pane)
		{
			_dockPane = pane;

			SetStyle(ControlStyles.OptimizedDoubleBuffer |
				ControlStyles.ResizeRedraw |
				ControlStyles.UserPaint |
				ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.Selectable, false);
		}
		#endregion

		#region Properties

		/// <summary>
		/// DockPane
		/// </summary>
		protected DockPane DockPane
		{
			get { return _dockPane; }
		} DockPane _dockPane;
		/// <summary>
		/// Appearance
		/// </summary>
		protected DockPane.AppearanceStyle Appearance
		{
			get { return DockPane.Appearance; }
		}
		/// <summary>
		/// HasTabPageContextMenu
		/// </summary>
		protected bool HasTabPageContextMenu
		{
			get { return DockPane.HasTabPageContextMenu; }
		}

		#endregion

		#region Methods

		/// <summary>
		/// ShowTabPageContextMenu
		/// </summary>
		/// <param name="position">Point</param>
		protected void ShowTabPageContextMenu(Point position)
		{
			DockPane.ShowTabPageContextMenu(this, position);
		}
		
		/// <summary>
		/// OnMouseUp
		/// </summary>
		/// <param name="e">MouseEventArgs</param>
		protected override void OnMouseUp(MouseEventArgs e)
		{
			base.OnMouseUp(e);

			if (e.Button == MouseButtons.Right)
				ShowTabPageContextMenu(new Point(e.X, e.Y));
		}
		
		/// <summary>
		/// OnMouseDown
		/// </summary>
		/// <param name="e">MouseEventArgs</param>
		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);

			if (e.Button == MouseButtons.Left &&
				DockPane.DockPanel.AllowEndUserDocking &&
				DockPane.AllowDockDragAndDrop &&
				!DockHelper.IsDockStateAutoHide(DockPane.DockState) &&
				DockPane.ActiveContent != null)
				DockPane.DockPanel.BeginDrag(DockPane);
		}

		/// <summary>
		/// WndProc
		/// </summary>
		/// <param name="m">Message</param>
		[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		protected override void WndProc(ref Message m)
		{
			if (m.Msg == (int)Msgs.WM_LBUTTONDBLCLK)
			{
				if (DockHelper.IsDockStateAutoHide(DockPane.DockState))
				{
					DockPane.DockPanel.ActiveAutoHideContent = null;
					return;
				}

				if (DockPane.IsFloat)
					DockPane.RestoreToPanel();
				else
					DockPane.Float();
			}
			base.WndProc(ref m);
		}

		/// <summary>
		/// OnRightToLeftLayoutChanged
		/// </summary>
		protected virtual void OnRightToLeftLayoutChanged()
		{
		}

		/// <summary>
		/// OnRefreshChanges
		/// </summary>
		protected virtual void OnRefreshChanges()
		{
		}

		/// <summary>
		/// MeasureHeight
		/// </summary>
		/// <returns>int</returns>
		protected internal abstract int MeasureHeight();

		internal void RefreshChanges()
		{
			if (IsDisposed)
				return;

			OnRefreshChanges();
		}

		#endregion
	}
	#endregion
}